package org.jeecg.modules.common.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.qyxx.entity.CsPtQyxx;
import org.jeecg.modules.qyxx.service.ICsPtQyxxService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserDepart;
import org.jeecg.modules.system.model.DepartIdModel;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.xxgx.kpzdgl.entity.CsXxKpzd;
import org.jeecg.modules.xxgx.kpzdgl.service.ICsXxKpzdsqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 获取当前登录用户信息工具类
 */
@Component
public class LoginUserUtil {
    private static final int DEF_DIV_SCALE=10;

    private static LoginUserUtil loginUserUtil;

    @Autowired
    private ICsXxKpzdsqService kpzdsqService;
    @Autowired
    private ICsPtQyxxService qyxxService;

    @Autowired
    private ISysUserDepartService sysUserDepartService;

    @Autowired
    private ISysDepartService sysDepartService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 加载容器中资源
     * 项目启动生成调用
     */
    @PostConstruct
    private void init () {
        loginUserUtil = this;
    }

    /**
     * 获取当前登录用户信息
     * @return
     */
    public static LoginUser getLoginUser(){
        LoginUser user=(LoginUser) SecurityUtils.getSubject().getPrincipal();
        return user;
    }

    /**
     * 获取当前登录用户所在企业信息
     * @return
     */
    public static CsPtQyxx getQyxxByLoginUser(){
        LoginUser user=(LoginUser) SecurityUtils.getSubject().getPrincipal();
        String companyid = user.getCompanyId();
        CsPtQyxx qyxx=loginUserUtil.qyxxService.getById(companyid);
        return qyxx;
    }

    /**
     * 获取当前登录用户所在企业信息授权的开票终端信息
     * @return
     */
    public static List<CsXxKpzd> getKpzdsByUserKpzdsq(String zdlx){
        LoginUser user=(LoginUser) SecurityUtils.getSubject().getPrincipal();
        return loginUserUtil.kpzdsqService.getKpzdsByUserKpzdsq(user.getUsername(),user.getCompanyId(),null,zdlx);
    }

    /**
     * 获取当前登录用户管辖的企业信息列表
     * @return
     */
    public static List<CsPtQyxx> getQyxxListByLoginUser(){
        LoginUser user=(LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<DepartIdModel>  departIdModels = loginUserUtil.sysUserDepartService.queryDepartIdsOfUser(user.getId());
        List<String> departIdList = new ArrayList<>();
        if(departIdModels != null){
            for(DepartIdModel departIdModel : departIdModels){
                departIdList.add(departIdModel.getKey());
            }
        }
        LambdaQueryWrapper<CsPtQyxx> queryQyxx = new LambdaQueryWrapper<CsPtQyxx>();
        queryQyxx.in(CsPtQyxx::getZzjgbm,departIdList);
        List<CsPtQyxx> qyxxList = loginUserUtil.qyxxService.list(queryQyxx);
        return qyxxList;
    }

    /**
     * 根据当前登录用户 取管辖部门的 所有用户
     * @return
     */
    public static List<SysUser> getUserListByLoginUser(){
        LoginUser user=(LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<DepartIdModel>  departIdModels = loginUserUtil.sysUserDepartService.queryDepartIdsOfUser(user.getId());
        List<String> departIdList = new ArrayList<>();
        if(departIdModels != null){
            for(DepartIdModel departIdModel : departIdModels){
                departIdList.add(departIdModel.getKey());
            }
        }
        QueryWrapper<SysUserDepart> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.in("dep_id",departIdList);
        List<SysUserDepart> list2 = loginUserUtil.sysUserDepartService.list(queryWrapper2);
        List<String> userids = list2.stream().map(SysUserDepart::getUserId).distinct().collect(Collectors.toList());
        QueryWrapper<SysUser> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.in("id",userids);
        List<SysUser> list1 = loginUserUtil.sysUserService.list(queryWrapper3);
//        QueryWrapper<SysDepart> queryWrapper = new QueryWrapper<>();
//        queryWrapper.in("id",departIdList);
//        List<SysDepart> list = loginUserUtil.sysDepartService.list(queryWrapper);
//        List<String> orgcodes = list.stream().map(SysDepart::getOrgCode).collect(Collectors.toList());
//        QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper<>();
//        queryWrapper1.in("org_code",orgcodes);
//        List<SysUser> list1 = loginUserUtil.sysUserService.list(queryWrapper1);
        return list1;
    }

    /**
     * 取当前登录用户 管辖的所有机构  orgcode
     * @return
     */
    public static List<String> getOrgCodesByLoginUser(){
        LoginUser user=(LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<DepartIdModel>  departIdModels = loginUserUtil.sysUserDepartService.queryDepartIdsOfUser(user.getId());
        List<String> departIdList = new ArrayList<>();
        if(departIdModels != null){
            for(DepartIdModel departIdModel : departIdModels){
                departIdList.add(departIdModel.getKey());
            }
        }
        QueryWrapper<SysDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",departIdList);
        List<SysDepart> list = loginUserUtil.sysDepartService.list(queryWrapper);
        List<String> orgcodes = list.stream().map(SysDepart::getOrgCode).collect(Collectors.toList());
        return orgcodes;
    }
}
